<?php
class controller_content extends controller_abstract
{
	private $pagesize = 15,$offset = 2;
	function __construct(& $app)
	{
		parent::__construct($app);
		$this->content =  loader::model('content');
	}
	
	function index()
	{
		$page = isset($_GET['page']) ? intval($_GET['page']) : 0;
		$pagesize = isset($_GET['pagesize']) ? intval($_GET['pagesize']) : $this->pagesize;
		$where = isset($_GET['missionid'])?"`#table_content`.`missionid`=$_GET[missionid]":'';
		$order = '`#table_content`.`contentid` DESC';
		$data = $this->content->ls($where, '*', $order, $page, $pagesize);
		$this->view->assign('data', $data);
		$this->view->assign(array('total'=> $this->content->total,'page'=>$page,'pagesize'=>$pagesize, 'offset'=>$this->offset));
		$this->view->display('content');
	}
	
	function view()
	{
		$contentid = $_GET['contentid'];
		$picture = loader::model('picture');
		$r = $picture->get_by_contentid($contentid);
		echo $this->json->encode($r);
	}
	
	function fetch()
	{
		$contentid = $_GET['contentid']; //获取当前contentid
		$contentids = explode(',',$contentid);
		$contenttotal = count($contentids);
		$contentnum = isset($_GET['contentnum'])?$_GET['contentnum']:0;
		$cur_contentid = $contentids[$contentnum]; 
		
		$content_data = $this->content->get($cur_contentid); //内容信息
		$mission_data = table('mission',$content_data['missionid']); //任务信息
		
		if($contentnum>=$contenttotal)
		{
			$this->showmessage('所有内容采集完成！',url('caitu/content/index',array('missionid'=>$content_data['missionid'])));
		}
		if(table('content',$cur_contentid,'fetched'))
		{
			$this->showmessage('此文章已采集',url('caitu/content/fetch',array('contentid'=>$contentid,'contentnum'=>++$contentnum)));
		}

		//获取总页数
		$content = file_get_contents($content_data['url']);
		
		$charset = $this->config['charset'];//编码转换
		if($mission_data['charset'] != $charset)
		{
			$content = str_charset($mission_data['charset'], $charset, $content);
		}
		if($mission_data['pagenumrule'])
		{
			$pagenumrule_reg = str2reg($mission_data['pagenumrule']);
			preg_match($pagenumrule_reg, $content, $matches);
			$total = (int) $matches[1];

			$url_reg = substr_replace($content_data['url'],$mission_data['pagerule'],strrpos($content_data['url'],'.'),1);
			$pages = array();
			for($i=1;$i<$total;$i++)
			{
				$pages[] = str_replace('{num}', $i , $url_reg);
			}
		}
		else 
		{
			$pages[] = $r['index'];
		}
		cache_write('content/'.$cur_contentid.'_pages.php', $pages);
		$this->showmessage('分页列表已缓存，开始采集',url('caitu/content/fetch_pages',array('cur_contentid'=>$cur_contentid,'total'=>$total,'contentid'=>$contentid,'contentnum'=>$contentnum)));
	}
	
	function fetch_pages()
	{
		$cur_contentid = $_GET['cur_contentid'];
		$total = $_GET['total'];
		$now = isset($_GET['now'])? $_GET['now'] : 0;
		
		$pages = cache_read('content/'.$cur_contentid.'_pages.php');
		
		$r = $this->content->get($cur_contentid);
		$mission_data = table('mission',$r['missionid']);
		
		if(!$now)
		{
			$content = file_get_contents($r['url']);
		}
		else
		{
			$content = file_get_contents($pages[$mission_data['page_coincide']?$now:$now-1]);
		}
		
		$charset = $CONFIG['charset'];//编码转换
		if($mission_data['charset'] != $charset)
		{
			$content = str_charset($mission_data['charset'], $charset, $content);
		}
		preg_match_all('/<img\s+[^>]*src\s*=\s*(["\']?)([^>"\']+)\1[^>]*[\/]?>/isU', $content, $imgs);

		$picoffset = table('mission', $r['missionid'], 'picoffset');
		if(strpos($picoffset, '-'))
		{
			$offset = explode('-',$picoffset);
			$start = $offset[0]-1;
			$length = $offset[1]-$offset[0]+1;
		}
		else 
		{
			$start = $picoffset-1;
			$length = 1;
		}
		$imgs = array_slice($imgs[2], $start, $length);
		
		$save_picture = $SYSTEM['save_picture']; //需要配置的地方
		
		if($save_picture)
		{
			import('attachment.download');
			$download = new download();
		}
		$picture = loader::model('picture');
		
		foreach ($imgs as $v)
		{
			$path = $save_picture ? $download->by_file($v) : NULL;
			$dd = array('missionid'=>$r['missionid'],
			'contentid'=>$cur_contentid,
			'path'=>$path,
			'url'=>$v,
			'description'=>null
			);
			$picture->insert($dd);
		}
		
		if($pagetype == 'uncoincide' ?$now<=$total : $now<$total)
		{
			$this->showmessage($r['title'].'_'.++$now.'采集成功！',url('caitu/content/fetch_pages',array('cur_contentid'=>$cur_contentid,'total'=>$total,'now'=>$now,'contentid'=>$_GET['contentid'],'contentnum'=>$_GET['contentnum'])),0);
		}
		else 
		{
			$this->content->update(array('fetched'=>'1'), "contentid=$cur_contentid");
			$_GET['contentnum']++;
			$this->showmessage('此内容所有图片采集完毕！',url('caitu/content/fetch',array('contentid'=>$_GET['contentid'],'contentnum'=>$_GET['contentnum'])));
		}
	}
	
	function refetch()
	{
		$contentid = $_GET['contentid'];
		$this->content->update(array('fetched'=>0), $contentid);
		$picture = loader::model('picture');
		$picture->delete_picture($contentid);
		if($picture->delete_by_contentid($contentid))
		{
			$this->showmessage('已采集数据已删除!开始重新采集...',url('caitu/content/fetch',array('contentid'=>$contentid)));
		}
	}
	
	function delete()
	{
		$contentid = $_GET['contentid'];
		if(!loader::model('picture')->delete_picture($contentid))
		{
			$this->showmessage($this->error(), 'back');
		}
		if($this->content->delete($contentid))
		{
			$this->showmessage('删除成功！', $_SERVER['HTTP_REFERER']);
		}
	}
	
}